﻿using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using AppTouchScreen.Imaging;

namespace AppTouchScreen.Data
{
    /// <summary>
    /// Provide save and load a data on the disk
    /// </summary>
    public class StoreRepository
    {
        /// <summary>
        /// The file for save data tree
        /// </summary>
        private const string DataFile = "DataFile.dat";

        /// <summary>
        /// Load data tree from the special file
        /// </summary>
        /// <returns></returns>
        public Category Load()
        {
            Category result = null;
            if (File.Exists(DataFile))
            {
                result = LoadFromFile(DataFile);
            }
            else
            {
                result = BuildFirs();
                ImageInfo.ClearNotUsing();
            } 
            return result;
        }

        /// <summary>
        /// Save data tree into the special file
        /// </summary>
        /// <param name="category">A root of data tree</param>
        public void Save(Category category)
        {
            var fs = new FileStream(DataFile, FileMode.Create);
            var formatter = new BinaryFormatter();
            try
            {
                formatter.Serialize(fs, category);
            }
            catch (SerializationException e)
            {
                throw new Exception("Failed to serialize. Reason: " + e.Message);
            }
            finally
            {
                fs.Close();
            }
            
        }

        /// <summary>
        /// Load data tree from files
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns>A root category</returns>
        private Category LoadFromFile(string fileName)
        {
            Category result = null;
            var fs = new FileStream(DataFile, FileMode.Open);
            var formatter = new BinaryFormatter();
            try
            {
                result = (Category)formatter.Deserialize(fs); //.Serialize(fs, category));
            }
            catch (SerializationException e)
            {
                throw new Exception("Failed to serialize. Reason: " + e.Message);
            }
            finally
            {
                fs.Close();
            }
            return result;
        }

        /// <summary>
        /// Create data tree for presentation features
        /// </summary>
        /// <returns>A root category</returns>
        private Category BuildFirs()
        {
            const string defaultCategory = "c.temp.png";
            const string defaultProduct = "p.temp.jpg";
            string imageFolder = Path.GetFullPath("..\\..\\..\\StructData\\") ;

            Properties.Resources.DefaultCategory.Save(defaultCategory);
            ImageInfo categoryImage = ImageInfo.CreateImageInfo(defaultCategory);

            Properties.Resources.DefaultProduct.Save(defaultProduct);
            ImageInfo productImage = ImageInfo.CreateImageInfo(defaultProduct);

            File.Delete(defaultCategory);
            File.Delete(defaultProduct);

            var root = new Category(null);
            root.Name = "Auto Salon";
            root.Description = "It’s auto salon description";
            root.Image = ImageInfo.CreateImageInfo(imageFolder + "20091920x1440.jpg");

            #region Cars

            Category catCars = new Category(root);
            catCars.Name = "Cars";
            catCars.Description = "These are own cars";
            catCars.Image = ImageInfo.CreateImageInfo(imageFolder
                + "Cars\\Maruti-Suzuki-CNG-cars.jpg");

            #region Minivens

            string minivansCatalog = imageFolder + "Cars\\Minivans\\";

            Category catMinivans = new Category(catCars);
            catMinivans.Name = "Minivans";
            catMinivans.Description = "These are own minivans";
            catMinivans.Image = ImageInfo.CreateImageInfo(minivansCatalog + 
                "Website photo.jpg");

            Product p = null;
            for(int i =0; i < 25; i++)
            {
                p = new Product(catMinivans);
                p.Name = "Dodge Caravan";
                p.Description = "2.4 L four-cylinder";
                p.Price = "$80,000";
                p.Image = ImageInfo.CreateImageInfo(minivansCatalog + 
                    "Dodge Caravan\\1998-dodge-caravan-1600x1200-image-1.jpg");

                p = new Product(catMinivans);
                p.Name = "Honda Odyssey";
                p.Description = "The 3.5-liter iVTEC";
                p.Price = "$43,250";
                p.Image = ImageInfo.CreateImageInfo(minivansCatalog +
                    "Honda Odyssey\\2011_Odyssey_002_Touring12.jpg");
            }

            #endregion //Minivens

            #region Sport Cars

            string sportCarsCatalog = imageFolder + "Cars\\Sports Cars\\";

            var catSportCar = new Category(catCars);
            catSportCar.Name = "Sport Cars";
            catSportCar.Description = "These are own sport cars";
            catSportCar.Image = ImageInfo.CreateImageInfo(sportCarsCatalog + 
                "sport car.jpg");

            var pBMWM5Concept = new Product(catSportCar);
            pBMWM5Concept.Name = "BMW M5 Concept";
            pBMWM5Concept.Description = "Wheels	20-inch forged rims";
            pBMWM5Concept.Price = "90 000$";
            pBMWM5Concept.Image = ImageInfo.CreateImageInfo(sportCarsCatalog +
                "BMW M5 Concept\\bmw-cs-concept.jpg");

            var pFerrariFF = new Product(catSportCar);
            pFerrariFF.Name = "Ferrari FF";
            pFerrariFF.Description = "683 Nm at 6000 rpm";
            pFerrariFF.Price = "120 000$";
            pFerrariFF.Image = ImageInfo.CreateImageInfo(sportCarsCatalog +
                "Ferrari FF\\Ferrari-FF-2.jpg");

            var pLamborghiniAventadorLP7004 = new Product(catSportCar);
            pLamborghiniAventadorLP7004.Name = "Lamborghini Aventador LP700-4";
            pLamborghiniAventadorLP7004.Description = "Official Top Speed	217 mph";
            pLamborghiniAventadorLP7004.Price = "$379,700";
            pLamborghiniAventadorLP7004.Image = ImageInfo.CreateImageInfo(sportCarsCatalog +
                "Lamborghini Aventador LP700-4\\lamborghini_aventador_LP_700-4_12.jpg");

            var pLLamborghiniGallardoLP5704 = new Product(catSportCar);
            pLLamborghiniGallardoLP5704.Name = "Lamborghini Gallardo LP 570-4";
            pLLamborghiniGallardoLP5704.Description = "Wheelbase	2560 mm (100,8 in)";
            pLLamborghiniGallardoLP5704.Price = "$248,700";
            pLLamborghiniGallardoLP5704.Image = ImageInfo.CreateImageInfo(sportCarsCatalog +
                "Lamborghini Gallardo LP 570-4 Superleggera\\Lamborghini-Gallardo-LP570-4-Superleggera_2011_04.jpg");

            var pNissanGTR = new Product(catSportCar);
            pNissanGTR.Name = "Nissan GT-R";
            pNissanGTR.Description = "Acceleration 0-60 mph s:	~3.0 seconds";
            pNissanGTR.Price = "$89,950";
            pNissanGTR.Image = ImageInfo.CreateImageInfo(sportCarsCatalog +
                "Nissan GT-R\\0.jpg");

            var pPaganiHuayra = new Product(catSportCar);
            pPaganiHuayra.Name = "Pagani Huayra";
            pPaganiHuayra.Description = "Mercedes-AMG M158 V12 twin-Turbo";
            pPaganiHuayra.Price = "$112,900";
            pPaganiHuayra.Image = ImageInfo.CreateImageInfo(sportCarsCatalog +
                "Pagani Huayra\\382-2011-pagani-huayra.jpg");

            var pPorsche911GT3RS = new Product(catSportCar);
            pPorsche911GT3RS.Name = "Porsche 911 GT3 RS 4.0";
            pPorsche911GT3RS.Description = "4.0 liters 6-cylinder";
            pPorsche911GT3RS.Price = "$185,000";
            pPorsche911GT3RS.Image = ImageInfo.CreateImageInfo(sportCarsCatalog +
                "Porsche 911 GT3 RS 4.0\\Porsche-911_GT3-RS_4-Coupe-Image-03.jpg");

            #endregion
            //Product p2 = new Product(cat1);
            //p2.Name = "p2";
            //p2.Description = "p2 Descriptions";
            //p2.Text = "p2 Text Text Text";
            //p2.Image = productImage;

            //Product p3 = new Product(cat1);
            //p3.Name = "p3";
            //p3.Description = "p3 Descriptions";
            //p3.Text = "p3 Text Text Text";
            //p3.Image = productImage;

            //Category cat2 = new Category(cat1);
            //cat2.Name = "category 2";
            //cat2.Description = "desc cat 2";
            //cat2.Image = categoryImage;
            //Product p = null;
            //for (int i = 0; i < 100; i++)
            //{
            //    p = new Product(cat2);
            //    p.Name = "p " + i.ToString();
            //    p.Description = p.Name + " desc";
            //    p.Text = i.ToString() + "  Text";
            //    p.Image = productImage;
            //}

            #endregion //Cars

            #region Auto parts

            string avtoPartCatalog = imageFolder + "Auto Parts\\";

            Category catAutoParts = new Category(root);
            catAutoParts.Name = "Auto Parts";
            catAutoParts.Description = "These are own Auto Parts";
            catAutoParts.Image = ImageInfo.CreateImageInfo(avtoPartCatalog +
                "modp_0907.jpg");

            //var pGarageProWheelCover = new Product(catAutoParts);
            //catAutoParts.Name = "Garage Pro Wheel Cover";
            //catAutoParts.Description = "Material: Plastic";
            //catAutoParts.Image = ImageInfo.CreateImageInfo(avtoPartCatalog +
            //    "\\Garage Pro Wheel Cover\\modp_0907.jpg");

            #endregion

            return root;
        }


    }
}
